<?php
namespace app\index\controller;
use think\Db;
use think\Request;
use think\Controller;
use cmf\controller\HomeBaseController;
use app\index\model\ApiModel;
//use app\api\model\ApiModel;
class ApiController extends HomeBaseController
{
    public function newget(){
		$data=Request::instance()->post();
      	$request = Request::instance();
      	$type=$data['game_types'];
      	$gamenum=$data['number'];
      	$xzmoney=$data['money'];
      	//判断用户金额是否足够下注
      	$uid=session('userid');
      	$userinfo=Db::name('user');
      	$user=$userinfo->where('id',$uid)->find();
      	if($xzmoney<2){
        	$msg=['code'=>0,'info'=>'最低下注2元哟！'];
            echo json_encode($msg);
            exit;
        }
      	if($user['balance']<$xzmoney){
        		$msg=['code'=>0,'info'=>'您的余额不足你的下注！'];
              	echo json_encode($msg);
            	exit;
        }
      	
      	//减去用户下注金额
      	$userjine=Db::name('user')->where('id', $uid)->setDec('balance', $xzmoney);
      	//判断提交游戏类型
      	if($type==0){
        	if(is_array($gamenum)){
              	if(count($gamenum)>10){
                	$msg=['code'=>0,'info'=>'下注信息错误！'];
                    json_encode($msg);
                    exit;
                }
              	//将下注内容插入数据库
              	$xz_moneys=$xzmoney/count($gamenum);
              		if($xz_moneys<2){
                        $msg=['code'=>0,'info'=>'最低下注2元哟！'];
                        echo json_encode($msg);
                        exit;
                    }
              	$openlist=Db::name('opend')->order("id desc")->limit(1)->select()->toArray();
              	$xz_term=$openlist[0]['term']+1;
              	foreach($gamenum as $value){
                  	$kj_or=array();
                	$kj_or['user']=$uid;
                  	$kj_or['account']=$value;
                    $kj_or['state']=0;
                    $kj_or['money']=$xz_moneys;
                  	$kj_or['addtime']=date("Y-m-d H:i:s"); 
                  	$kj_or['orders']='R'.date("YmdHis").mt_rand(1111,9999);
                  	$kj_or['ip']=$request->ip();
                  	$kj_or['zj']=0.00;
                  	$kj_or['term']=$xz_term;
                    Db::name('orders')->insert($kj_or);
                }
              	$jsonarr=array(
                	'gamenum'=>$gamenum,
                  	'money'=>$xz_moneys,
                  	'code'=>1,
                  	'info'=>"投注成功",	
                  	'term'=>$xz_term
                );
              	echo json_encode($jsonarr);
              	
            }else{
              	$msg=['code'=>0,'info'=>'下注信息错误！'];
              	json_encode($msg);
            	exit;
            }
        }elseif($type==1){
        	if(is_array($gamenum)){
              	if(count($gamenum)>50){
                	$msg=['code'=>0,'info'=>'下注信息错误！'];
                    json_encode($msg);
                    exit;
                }
              	//将下注内容插入数据库
              	$xz_moneys=$xzmoney/count($gamenum);
              	if($xz_moneys<2){
                        $msg=['code'=>0,'info'=>'最低下注2元哟！'];
                        echo json_encode($msg);
                        exit;
                    }
              	$openlist=Db::name('opend')->order("id desc")->limit(1)->select()->toArray();
              	$xz_term=$openlist[0]['term']+1;
              	foreach($gamenum as $value){
                  	$kj_or=array();
                	$kj_or['user']=$uid;
                  	$kj_or['account']=$value;
                    $kj_or['state']=0;
                    $kj_or['money']=$xz_moneys;
                  	$kj_or['addtime']=date("Y-m-d H:i:s"); 
                  	$kj_or['orders']='R'.date("YmdHis").mt_rand(1111,9999);
                  	$kj_or['ip']=$request->ip();
                  	$kj_or['zj']=0.00;
                  	$kj_or['term']=$xz_term;
                    Db::name('orders')->insert($kj_or);
                }
              	$jsonarr=array(
                	'gamenum'=>$gamenum,
                  	'money'=>$xz_moneys,
                  	'code'=>1,
                  	'info'=>"投注成功",	
                  	'term'=>$xz_term
                );
              	echo json_encode($jsonarr);
            }else{
            	$msg=['code'=>0,'info'=>'下注信息错误！'];
              	json_encode($msg);
            	exit;
            }
        }elseif($type==2){
        	if(!is_array($gamenum)){
              		$openlist=Db::name('opend')->order("id desc")->limit(1)->select()->toArray();
              		$xz_term=$openlist[0]['term']+1;
                  	$kj_or=array();
                	$kj_or['user']=$uid;
                  	$kj_or['account']=$gamenum;
                    $kj_or['state']=0;
                    $kj_or['money']=$xzmoney;
                  	$kj_or['addtime']=date("Y-m-d H:i:s"); 
                  	$kj_or['orders']='R'.date("YmdHis").mt_rand(1111,9999);
                  	$kj_or['ip']=$request->ip();
                  	$kj_or['zj']=0.00;
                  	$kj_or['term']=$xz_term;
                    Db::name('orders')->insert($kj_or);
              		$jsonarr=array(
                	'gamenum'=>$gamenum,
                  	'money'=>$xzmoney,
                  	'code'=>1,
                  	'info'=>"投注成功",	
                  	'term'=>$xz_term
                    );
                    echo json_encode($jsonarr);
            }else{
            	$msg=['code'=>0,'info'=>'下注信息错误！'];
              	json_encode($msg);
            	exit;
            }
        }elseif($type==3){
        	if(is_array($gamenum)){
              	if(count($gamenum)>10){
                	$msg=['code'=>0,'info'=>'下注信息错误！'];
                    json_encode($msg);
                    exit;
                }
              	//将下注内容插入数据库
              	$xz_moneys=$xzmoney/count($gamenum);
              	if($xz_moneys<2){
                        $msg=['code'=>0,'info'=>'最低下注2元哟！'];
                        echo json_encode($msg);
                        exit;
                 }
              	$openlist=Db::name('opend')->order("id desc")->limit(1)->select()->toArray();
              	$xz_term=$openlist[0]['term']+1;
              	foreach($gamenum as $value){
                  	$kj_or=array();
                	$kj_or['user']=$uid;
                  	$kj_or['account']=$value;
                    $kj_or['state']=0;
                    $kj_or['money']=$xz_moneys;
                  	$kj_or['addtime']=date("Y-m-d H:i:s"); 
                  	$kj_or['orders']='R'.date("YmdHis").mt_rand(1111,9999);
                  	$kj_or['ip']=$request->ip();
                  	$kj_or['zj']=0.00;
                  	$kj_or['term']=$xz_term;
                    Db::name('orders')->insert($kj_or);
                }
              	 $jsonarr=array(
                      'gamenum'=>$gamenum,
                      'money'=>$xz_moneys,
                      'code'=>1,
                      'info'=>"投注成功",	
                      'term'=>$xz_term
                    );
                 echo json_encode($jsonarr);
            }else{
            	$msg=['code'=>0,'info'=>'下注信息错误！'];
              	json_encode($msg);
            	exit;
            }
        }
      
      	//增加佣金
         	$conf=cmf_get_option("site_info");	
          	$u=Db::name('user')->where(['id'=>$uid])->find();
              for($i=0;$i<4;$i++){
               $price=$xzmoney*$conf['site_admin_yj'.$i]/100;
               $u1=Db::name('user')->where(['id'=>$u['ss'.$i]])->find();
                  if($u1){
                    Db::name('user')->where(['id'=>$u['ss'.$i]])->setInc("score",$price);
                    $datas['user']=$u['ss'.$i];
                    $datas['money']=$price;
                    $datas['addtime']=date("Y-m-d H:i:s");
                    $datas['account']=($i+1)."级返佣";
                    Db::name('yjjl')->insert($datas);
                  }
              }
         //增加佣金结束
      
    }
	
	public function notify(){
	   $data=Request::instance()->get();
	   $arr['key']=$data['key'];
	   $arr['fujian']=$data['record'];
	   $arr['money']=$data['money'];
	   $arr['money_index']=$data['money_index'];
	   $co=(new Config)->where(['id'=>1])->find();
	   echo $co['kkey'];
	   if($data['key']==$co['kkey']){
		   (new Pay)-> paynot($arr);
	   }else{
		   
		   echo "keyerror";
	   }
   }
	
	public function open(){
      	//查询数据库中是否达到开奖时间
      	$openlist=Db::name('opend')->order("id desc")->limit(1)->select()->toArray();
		if($openlist){
      	  //上期开奖时间
          if(time()-$openlist[0]['addtime']>60){
             //查询是否已经存入记录;
              $url ='http://api.b1api.com/api?p=json&t=txffc&limit=3&token=c8cdbbfb94a55b0b';
              $ch = curl_init();
              curl_setopt($ch, CURLOPT_URL, $url);
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
              $output = curl_exec($ch);
              curl_close($ch);
              //打印获得的数据
              $msg = json_decode($output,true);
              if($msg){
                $ingopen=Db::name('opend')->where('term',$msg['data'][0]['expect'])->find();
                if(!$ingopen){
                   	$addtime = strtotime($msg['data'][0]['opentime']);
                    $opens=array();
                    $opens['account']="txffc";
                    $opens['money']=0.00;
                    $opens['addtime']=$addtime;
                    $opens['term']=$msg['data'][0]['expect'];
                    $opens['num']=$msg['data'][0]['opencode'];
                    Db::name('opend')->insert($opens);
                  	$addtime=$addtime+60;
                  	$addtime=$addtime-time();
                  	$msg=['code'=>0,'info'=>$addtime];
                    echo json_encode($msg);
                }
              }
          }else{
            $kjtimes=$openlist[0]['addtime']+60;
            $kjtimes=$kjtimes-time();
            $msg=['code'=>0,'info'=>$kjtimes];
            echo json_encode($msg);
          }
        
        }else{
          	//判断数据里面没有记录则直接插入
        	$url ='http://api.b1api.com/api?p=json&t=txffc&limit=3&token=c8cdbbfb94a55b0b';
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($ch);
            curl_close($ch);
            //打印获得的数据
            $msg = json_decode($output,true);
          	$opens=array();
         	$opens['account']="txffc";
          	$opens['money']=0.00;
          	$opens['addtime']=$addtime;
          	$opens['term']=$msg['data'][0]['expect'];
          	$opens['num']=$msg['data'][0]['opencode'];
			Db::name('opend')->insert($opens);
        }
      	(new ApiModel)->opend();
     	//return $this->fetch('open');
      	
	}
  	public function getopen(){
		$data=Request::instance()->post();
      	$trem=$data['code'];
      	$info=Db::name('opend')->where('term',$trem+1)->find();
      	if($info){
        	$msg=['code'=>1];
          	$msg=json_encode($msg);
          	return $msg;
        }else{
        	$msg=['code'=>0];
          	$msg=json_encode($msg);
          	return $msg;
        }
	}
  	public function kaijiang(){
		(new ApiModel)->opend();
	}
	
}
